package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.result.JWTUtils;
import com.atguigu.commonutils.result.R;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.frontvo.CourseQueryVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.feign.OrderFeignService;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author kabuda
 * @create 2023-05-16 14:19
 */
@RestController
@RequestMapping("/eduservice/front/course")
public class FrontCourseController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrderFeignService orderFeignService;
    @PostMapping("{page}/{limit}")
    public R searchList(@PathVariable("page")Long page, @PathVariable("limit")Long limit,@RequestBody(required = false) CourseQueryVo courseQueryVo){
       Map<String ,Object> map = courseService.searchList(page, limit, courseQueryVo);
       return R.ok().data(map);
    }
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("courseInfo/{courseId}")
    public R courseInfo(@PathVariable("courseId")String courseId, HttpServletRequest request){
        CourseWebVo courseWebVo = courseService.FrontCourseInfo(courseId);
        List<ChapterVo> chapterVideos = chapterService.getChapterVideo(courseId);
        //增加访问量
        courseService.addViewCount(courseId);
        String token = request.getHeader("token");
        Map<String, Object> map = JWTUtils.checkToken(token);
        String memberId = (String) map.get("id");
        boolean b = orderFeignService.isBuy(courseId, memberId);

        return R.ok().data("course", courseWebVo).data("chapterVoList",chapterVideos).data("status",b).data("memberId",memberId);
    }
    @GetMapping("getCourse/{courseId}")
    public CourseWebVo getCourse(@PathVariable("courseId")String courseId){
        CourseWebVo courseWebVo = this.courseService.FrontCourseInfo(courseId);
        return  courseWebVo;
    }

}
